﻿using System;
using System.Windows.Controls;

using Perseus;

using Hetymine.Executioner.Data;

namespace Hetymine.Executioner.Commands {
    /// <summary>
    /// Wrapper class for old command configurator
    /// </summary>
    public class CommandConfiguratorItem : IConfiguratorItem {
        public CommandConfiguratorItem(string type, ICommand command) {
            this.Type = type;
            this.Command = command;
            this.Command.Configurator.CommandNameChanged += new EventHandler(Configurator_CommandNameChanged);
        }

        private void Configurator_CommandNameChanged(object sender, EventArgs e) {
            this.OnItemNameChanged(e);
        }

        public string Type { get; protected set; }
        public ICommand Command { get; protected set; }

        #region IConfiguratorItem
        public bool IsEmpty { get { return this.Command.IsEmpty; } }
        public string Name { get { return this.Command.Name; } }

        public bool IsMatch(string filter) {
            string[] filters = filter.ToLower().Split(" ", SplitOptions.RemoveEmptyEntries);
            
            foreach (string f in filters) {
                if (this.Name.ToLower().IndexOf(f) >= 0) {
                    continue;        
                }

                bool found = false;

                foreach (string s in this.Command.CallStrings) {
                    if (s.ToLower().IndexOf(f) >= 0) {
                        found = true;
                        break;
                    }
                }
                if (found) { continue; }

                return false;
            }

            return true;
        }

        public event EventHandler ItemNameChanged;
        protected virtual void OnItemNameChanged(EventArgs e) {
            if (this.ItemNameChanged != null) {
                this.ItemNameChanged(this, e);
            }
        }

        public UserControl ConfigPanel { 
            get { return this.Command.Configurator.ConfigPanel; } 
        }
        #endregion
    }
}
